<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="addPermission()">添加权限</el-button>
      <!--
      table 支持树形结构
      1. row-key="id" 树形节点添加唯一标识
      2. 默认展开
      -->
      <el-table default-expand-all row-key="id" :data="permissionList">
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <!-- 一级权限 type === 1 才能添加子权限 -->
          <!-- 二级权限 type === 2 控制页面中的按钮是否显示 / 禁用 -->
          <template #default="{row}">
            <el-button v-if="row.type === 1" size="mini" type="text" @click="addPermission(row.id)">添加</el-button>
            <el-button size="mini" type="text" @click="editPermission(row.id)">编辑</el-button>
            <el-popconfirm title="确定要删除吗？" @onConfirm="del(row.id)">
              <el-button slot="reference" size="mini" type="text" style="margin-left: 10px;">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :title="title" :visible="isShowDialog" @close="close">
      <!-- 表单 -->
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch v-model="formData.enVisible" active-value="1" inactive-value="0" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">确定</el-button>
          <el-button @click="close">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { addPermission, deletePermission, getPermissionDetail, getPermissionList, updatePermission } from '@/api/permission'
import { transListToTree } from '@/utils'

export default {
  name: 'Permission',
  data() {
    return {
      permissionList: [],
      formData: {
        name: '',
        code: '',
        description: '',
        type: null,
        pid: null,
        enVisible: 0
      },
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' }
        ]
      },
      title: '',
      isShowDialog: false,
      isEdit: false
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 获取权限列表
    async getData() {
      const res = await getPermissionList()
      // console.log(res)
      const list = await transListToTree(res, 0)
      this.permissionList = list
    },
    // 新增权限点
    async addPermission(id) {
      if (!id) {
        // 添加一级权限按钮
        this.title = '新增一级权限点'
        this.formData.type = 1
        this.formData.pid = 0
      } else {
        // 添加二级权限按钮
        this.title = '新增二级权限点'
        this.formData.type = 2
        this.formData.pid = id
      }
      this.isEdit = false
      this.isShowDialog = true
    },
    async editPermission(id) {
      this.title = '编辑权限点'
      this.formData = await getPermissionDetail(id)
      this.isEdit = true
      this.isShowDialog = true
    },
    // 确认-新增/编辑权限
    async submit() {
      // 发送新增/编辑请求
      if (this.isEdit) {
        await updatePermission(this.formData)
      } else {
        await addPermission(this.formData)
      }
      // 重新渲染
      this.getData()
      // 关闭弹层并清空数据
      this.close()
      // 提示用户
      this.$message.success('操作成功')
    },
    // 取消
    close() {
      this.isShowDialog = false
      this.$refs.formRef.resetFields()
      this.formData = {
        name: '',
        code: '',
        description: '',
        type: null,
        pid: null,
        enVisible: 0
      }
    },
    // 删除权限
    async del(id) {
      await deletePermission(id)
      this.getData()
      this.$message.success('删除成功')
    }
  }
}
</script>
<style>
.btn-add {
  margin: 10px;
}
</style>
